var strGetQuery =document.location.search;
var id = cw.common.getQueryString(strGetQuery,'id');
$(document).ready(function(){
	cw.common.setStyle();
	$('#subImg').bind('click',subButton);
	cw.common.getUserSession(getDepartmentList,true);
	validate();
})

function getDepartmentList(){
	
	var node = $('#DEPARTMENT_NO');
	
	$.post(cw.common.path + '/json?action=QUERY_T_DEPARTMENT_ACTION',{},function(data){
		if(data.success){
			$.each(eval(data.root.T_DEPARTMENT.rs),function(i,n){
				node.append('<option value="'+this.DEPARTMENT_NO+'">'+this.DEPARTMENT_NAME+'</option>');
			})
		} else {
			alert(data.errors.errmsg);
		}
	},'json');
	getStaff();
}

function getStaff(){
	var url = cw.common.path + '/json?action=GET_T_PERSONNAL_ACTION&PERSONNAL_NO='+id;
	$.post(url,{},function(data){
		if(data.success){
			var vobj=data.root.T_PERSONNAL;
			if(vobj==null||vobj==''||vobj==undefined){
				alert("该信息不存在或已被删除");
				window.history.back();
				return;
			}
			else{
				var obj = data.root.T_PERSONNAL.rs[0];
				cw.common.setValue("PERSONNAL_NO",id, "text");
				cw.common.setValue("NAME",obj.NAME, "text");
				cw.common.setValue("SEX",obj.SEX, "text");
				cw.common.setValue("DEPARTMENT_NO",obj.DEPARTMENT_NO, "text");
				cw.common.setValue("POSITION",obj.POSITION, "text");
				cw.common.setValue("JION_DATE",obj.JION_DATE, "text");
				cw.common.setValue("TELEPHONE",obj.TELEPHONE, "text");
				cw.common.setValue("REMARK",obj.REMARK, "text");
			}
		}
		else{
			alert(data.errors.errmsg);
		}
	},'json');
	
}


function subFrm(){
	var options = {
	    dataType: 'json',
    	iframe:	false,
	    success: function(data) {
	    	if(data.success){
	    		$('#subImg').bind('click',subButton);
	    		alert('已修改人员【'+$('#NAME').val()+'】');
	      		window.location.href="staff.html";
	      	}else{
	      		alert(data.errors.errmsg);
	      	}
	   	}
	};
	$.ajaxSetup({contentType:'application/x-www-form-urlencoded;charset=utf-8'});
    $('#frm').ajaxSubmit(options);
}

function subButton(){
	$('#subImg').unbind('click',subButton);
	return jQuery.formValidator.pageIsValid(1);
}

function validate(){
	$.formValidator.initConfig({formid:"frm",onerror:function(msg){$('#subImg').bind('click',subButton);alert(msg);},onsuccess:function(){subFrm();return false;}});
	$("#NAME").formValidator({onshow:"请输入人员姓名",oncorrect:"正确",onfocus:"姓名由英文或简体中文组成"})
	.inputValidator({min:1,onerror:"人员姓名必填"})
	.inputValidator({max:16,onerror:"真实姓名中文不超过8个字，英文不超过16个字符"})
	.functionValidator({
		fun:function (str,elem){
			str = $.trim(str);
			if(str.length == 0) return "人员姓名必填";
			var a = !str.match('^([a-zA-Z]|[\\u4E00-\\u9FA5\\uF900-\\uFA2D])+$');
			if(a) return "姓名4-16个字符，英文或简体中文组成";
			return true;
		}, onerror: "人员姓名错误"
	});
	$("#POSITION").formValidator({empty:true,onshow:"请填写职务",oncorrect:"正确",onfocus:"请填写职务"}).inputValidator({max:32,onerror:"职务名称过长"});
	//$("#JION_DATE").formValidator({onshow:"请填写入司时间",oncorrect:"正确",onfocus:"请填写入司时间"}).inputValidator({min:1,onerror:"请填写入司时间"});
	$("#TELEPHONE").formValidator({empty:true,onshow:"请填写联系电话",oncorrect:"正确",onfocus:"联系电话可以是手机或固话"}).inputValidator({max:64,onerror:"联系电话信息过长"});
	$("#REMARK").formValidator({empty:true,onshow:"请输入人员备注",oncorrect:"正确",onfocus:"请输入人员备注信息"}).inputValidator({max:100,onerror:"人员备注信息中文不超过50个字，英文不超过100个字符"});
}

